package com.zeng.createmd.restfulmodule.controller;

import com.zeng.createmd.restfulmodule.constant.DataSaveLocation;
import com.zeng.createmd.restfulmodule.convert.TableEntityConvertor;
import com.zeng.createmd.restfulmodule.itemCache.ItemCache;
import com.zeng.createmd.restfulmodule.util.ItemUtil;
import com.zeng.createmd.restfulmodule.util.JDBCUtil;
import com.zeng.createmd.restfulmodule.util.ProjectUtil;
import com.zeng.createmd.servicemodule.vo.DatabaseVo;
import com.zeng.nicecommon.util.ResponseUtil;
import com.zeng.plugin.entity.TableEntity;
import com.zeng.plugin.entity.TableRelative;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RequestMapping
@RestController
public class DataBaseController {

    @Autowired
    ItemUtil itemUtil;

    @RequestMapping
    public Object add(DatabaseVo databaseVo) {
        return null;
    }


    @RequestMapping
    public Object initDatabase(DatabaseVo databaseVo) {
        if (ItemCache.currentItem == null) {
            return ResponseUtil.badRequest("请选择项目");
        }
        try {
            JDBCUtil.createDatabase(databaseVo);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("创建数据库失败:" + e.getMessage());
        }
        itemUtil.addDatabaseV(databaseVo);
        return ResponseUtil.success("操作成功");
    }

    @RequestMapping
    public Object detail() {
        return ResponseUtil.success("操作成功").addData("detail", itemUtil.getDatabaseVo());
    }

    @RequestMapping
    public Object addTable(@RequestBody Map<String, Object> table) {
        ProjectUtil projectUtil = new ProjectUtil();
        List<TableEntity> tables = (List<TableEntity>)projectUtil.getData().get("tables");
        if (tables == null) {
            List<TableEntity> tableEntities = new ArrayList<>();
            tables = tableEntities;
            projectUtil.getData().put("tables", tables);

        }
        String tableName = (String) table.get("tableName");
        projectUtil.removeTable(tableName);
        tables.add(new TableEntityConvertor().mapToTableEntity(table));
        projectUtil.putTableMap(tableName, table);
        projectUtil.init();
        return ResponseUtil.success("操作成功");
    }

    @RequestMapping
    public Object tableList() {
        ProjectUtil projectUtil = new ProjectUtil();
        Object tables = projectUtil.getData().get("tables");
        return ResponseUtil.success("操作成功").addData("tables", tables);
    }
    @RequestMapping
    public Object deleteTable(String realTableName) {
        ProjectUtil projectUtil = new ProjectUtil();
        projectUtil.removeTable(realTableName);
        return ResponseUtil.success("操作成功");
    }

    @RequestMapping
    public Object detailTable(String tableName) {

        ProjectUtil projectUtil = new ProjectUtil();
        Object table = projectUtil.getTableMap(tableName);
        return ResponseUtil.success("操作成功").addData("table", table);
    }

    @RequestMapping
    public Object tableRelative(String tableName) {
        return null;
    }

    @RequestMapping
    public Object tableRelativeAll() {
        return ResponseUtil.success("操作成功").addData("list", new ProjectUtil().relativeList());
    }

    @RequestMapping
    public Object addTableRelative(@RequestBody TableRelative tableRelative) {

        ProjectUtil projectUtil = new ProjectUtil();
        projectUtil.createTableRelative(tableRelative);
        return ResponseUtil.success("操作成功");
    }

    @RequestMapping
    public Object deleteRelative(String table1, String table2) {
        ProjectUtil projectUtil = new ProjectUtil();
        projectUtil.removeTableRelativeValue(TableRelative.builder().table1(table1).table2(table2).build());
        return ResponseUtil.success("操作成功");
    }

    @RequestMapping
    public Object test(String table1, String table2) {
        ProjectUtil projectUtil = new ProjectUtil();
        projectUtil.removeTableRelativeValue(TableRelative.builder().table1(table1).table2(table2).build());
        return ResponseUtil.success("操作成功");
    }
}
